//
//  CommentCell.m
//  CarLife
//
//  Created by yu on 2019/4/2.
//  Copyright © 2019年 jpas. All rights reserved.
//

#import "CommentCell.h"
#import "CommentModel.h"

@implementation CommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    UIView *contentView = self.contentView;
    
    _iconImgView.sd_layout
    .leftSpaceToView(contentView, 16)
    .topSpaceToView(contentView, 16)
    .widthIs(kAutoWidth(36))
    .heightEqualToWidth();
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconTapAction:)];
    [_iconImgView addGestureRecognizer:tap];
    
    _nicknameLabel.sd_layout
    .leftSpaceToView(_iconImgView, 11)
    .topEqualToView(_iconImgView)
    .heightIs(20);
    [_nicknameLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    _contentLabel.sd_layout
    .leftEqualToView(_nicknameLabel)
    .rightSpaceToView(contentView, 16)
    .topSpaceToView(_nicknameLabel, 5)
    .autoHeightRatio(0);
    
    _allCommentBtn.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_contentLabel, 8);
    [_allCommentBtn setupAutoSizeWithHorizontalPadding:10 buttonHeight:24];
    
    _timeLabel.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_allCommentBtn, 8)
    .heightIs(20);
    [_timeLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    _likeBtn.sd_layout
    .rightSpaceToView(contentView, 10)
    .centerYEqualToView(_timeLabel)
    .heightIs(kAutoHeight(30))
    .widthIs(kAutoWidth(50));
    
    _commentBtn.sd_layout
    .rightSpaceToView(_likeBtn, 8)
    .centerYEqualToView(_likeBtn)
    .heightRatioToView(_likeBtn, 1)
    .widthRatioToView(_likeBtn, 1);
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kLineGrayColor;
    [self.contentView addSubview:lineView];
    
    lineView.sd_layout
    .leftEqualToView(_nicknameLabel)
    .bottomSpaceToView(contentView, 0)
    .rightSpaceToView(contentView, 0)
    .heightIs(0.5);
    
}

- (void)iconTapAction:(UITapGestureRecognizer *)sender {
    PPLog(@"点击头像了");
}

- (void)setCModel:(CommentModel *)cModel
{
    _cModel = cModel;
    
    _contentLabel.text = cModel.content;
    
    NSInteger secondCommentsCount = _cModel.secondCommentsArray.count;
    
    if (secondCommentsCount > 0) {
        _allCommentBtn.hidden = NO;
        _timeLabel.sd_layout.topSpaceToView(_allCommentBtn, 8);
        [_allCommentBtn setTitle:NSStringFormat(@"共%ld条回复>",secondCommentsCount) forState:UIControlStateNormal];
    } else {
        _allCommentBtn.hidden = YES;
        _timeLabel.sd_layout.topSpaceToView(_contentLabel, 8);
    }
    
    [self setupAutoHeightWithBottomView:_timeLabel bottomMargin:10];
}

- (IBAction)allCommentAction:(id)sender {
    NSInteger index = ((UIButton *)sender).tag;
    if (self.delegate && [self.delegate respondsToSelector:@selector(showAllComments:)]) {
        [self.delegate showAllComments:index];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
